package com.hctforgreen.greenservice.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.service.HctApplication;


/**
 * 提示类
 * 多功能的Dialog
 * Created by Yuniko on 2015/8/14.
 */
public class GroupDialog {
    private static Context context;
    private static Dialog dialog;
    private TextView titleTextView;
    private static HorizontalProgressBarWithNumber progressBarHor;
    private static RoundProgressBarWidthNumber progressBarRound;
    private Display display;
    private boolean showTitle = false;
    private boolean showIcon = false;
    private static boolean showMsg = false;
    private boolean showSelsetPanel = false;
    private boolean showCountDown = false;
    private static boolean showProgressBarHor = false;
    private static boolean showProgressBarRound = false;
    private static int progress = 0;
    Percent percent;

    /**
     * ************************************************* nomal AlertDialog *************************************
     */

    public GroupDialog(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.context = context;
    }

    public void setPercent(Percent percent){
        this.percent = percent;
    }

    public GroupDialog builder(int style) {

        //获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.view_alertdialog, null);
        //获取自定义Dialog布局中的控件
        titleTextView = (TextView) view.findViewById(R.id.view_alertdialog_textview_title);
        titleTextView.setVisibility(View.GONE);
        progressBarRound = (RoundProgressBarWidthNumber) view.findViewById(R.id.view_alertdialog_RoundProgressBarWidthNumber);
        progressBarRound.setVisibility(View.GONE);
        //定义Dialog布局的参数(默认)
        dialog = new Dialog(context, style);
        dialog.setContentView(view);
        if (style == R.style.ActionListDialogStyle) {
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            dialogWindow.setAttributes(lp);
        }
        return this;
    }

    //设置GroupDialog大小
    public GroupDialog setAlertDialogSize(double size) {
        return this;
    }
    //设置触摸外界不销毁
    public GroupDialog setCancleOnTouchOutside(boolean cancle) {
        dialog.setCanceledOnTouchOutside(cancle);
        return this;
    }

    //设置计时器
    public GroupDialog setCountDown(int time, String msg, final OnTimeAlertViewClickListener positiveListener) {
        showCountDown = true;
        timeCount = time;
        timeMessgae = msg;
        if ("".equals(msg)) {
            msgTextView.setText("正在加载");
        } else {
            msgTextView.setText(timeCount + timeMessgae);
        }
        handler.sendEmptyMessage(0);
        return this;
    }

    private static int timeCount;//倒计时时间
    private static String timeMessgae;//倒计时信息
    private static TextView msgTextView;
    //倒计时
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
                int what = msg.what;
                if (what == 0) {
                    if (timeCount > 0) {
                        timeCount--;
                        handler.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        handler.removeMessages(0);
                        dialog.dismiss();
                    }
                } else if (what == 1) {
                    if (progress < timeCount) {
                        // 指定增加的进度
                        if (showProgressBarHor)
                            progressBarHor.setProgress(5);
                        else{
                            i = HctApplication.percent;
                            Log.i("i","当前i//////"+i);
                            progressBarRound.setProgress(i);
                            handler.sendEmptyMessageDelayed(1, 1000);
                            if(!(i<100)){
                                handler.removeMessages(1);
                                dialog.dismiss();
                                LogUtil.i("HCTAPP","HctApplication.percent"+HctApplication.percent);
                                HctApplication.percent = 1;
                                return;
                            }
                        }

                    } else {
                        handler.removeMessages(1);
                        dialog.dismiss();
                    }
//                    handler.removeMessages(1);
            }
            super.handleMessage(msg);
        }
    };

    int i = 0 ;

    //调用时间接口
    public interface OnTimeAlertViewClickListener {
        public abstract void confirm();
    }

    //调用时间接口
    public interface Percent {
        public int percent();
    }


    //圆形进度条
    public GroupDialog setProgressBarRound(final int allsum) {
        showProgressBarRound = true;
        progressBarRound.setMax(allsum);
        progressBarRound.setIndeterminate(false);//设置显示明确的进度，true为不显示
        progressBarRound.getProgress();
        progress = progressBarRound.getProgress();
        timeCount = allsum;
        handler.sendEmptyMessage(1);
        return this;
    }
    /**
     * 一般方法
     */

    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public void dismiss() {
        showMsg = false;
        context = null;
        showProgressBarHor = false;
        showProgressBarRound = false;
        handler.removeMessages(0);
        handler.removeMessages(1);
        dialog.dismiss();
    }

    //设置整体布局
    private void setLayout() {

        //默认标题样式
        if (!showTitle && !showMsg && !showSelsetPanel &&  !showIcon) {
            titleTextView.setText("正在加载数据...请等待");
            titleTextView.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            titleTextView.setVisibility(View.VISIBLE);
        }
        if (showMsg) {
            msgTextView.setVisibility(View.VISIBLE);
        }
        if (showCountDown) {
            msgTextView.setVisibility(View.VISIBLE);
        }

        if (showProgressBarHor) {
            if (showMsg) {
            }
            progressBarHor.setVisibility(View.VISIBLE);
        }
        if (showProgressBarRound) {
            if (showMsg) {
            }
            progressBarRound.setVisibility(View.VISIBLE);
        }
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismiss();
            }
        });

    }

    //返回键监听，点击返回键无反应，弹窗提示
    public GroupDialog setCancleBack(final Context context){
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(keyCode== KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
                    doubleTouchExit(context);
                    return true;
                }else{
                    return false;
                }
            }
        });
        return this;
    }
    private void doubleTouchExit(Context context) {
        Toast.makeText(
                context,
                "请等待更新完成", Toast.LENGTH_SHORT)
                .show();
    }


}
